package com.punai.web.controller.system;

import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.core.domain.entity.SysPnDictionary;
import com.punai.system.service.SysPnDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/pndict")
public class SysPnDictController extends BaseController {

    @Autowired
    private SysPnDictionaryService sysPnDictionaryService;

    @PreAuthorize("@ss.hasPermi('system:pndict:list')")
    @GetMapping("treelist")
    public AjaxResult treelist() {
        List<TreeBo> result = sysPnDictionaryService.treelist(getCompanyId());
        return AjaxResult.success(result);
    }

    @PreAuthorize("@ss.hasPermi('system:pndict:info')")
    @GetMapping("/getNextCode/{pid}")
    public AjaxResult getNextCode(@PathVariable String pid) {
        return AjaxResult.success("操作成功",sysPnDictionaryService.getNextCode(pid,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('system:pndict:info')")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return AjaxResult.success("操作成功",sysPnDictionaryService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:pndict:add')")
    @PostMapping
    public AjaxResult add(@RequestBody SysPnDictionary vo) {
        vo.setCompanyId(getCompanyId());
        return toAjax(sysPnDictionaryService.insertPnDict(vo));
    }

    @PreAuthorize("@ss.hasPermi('system:pndict:edit')")
    @PutMapping
    public AjaxResult update(@RequestBody SysPnDictionary vo) {
        return toAjax(sysPnDictionaryService.updatePnDict(vo));
    }

    @PreAuthorize("@ss.hasPermi('system:pndict:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id) {
        return toAjax(sysPnDictionaryService.delete(id,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('system:pndict:reload')")
    @GetMapping("/reloadCache")
    public AjaxResult reloadCache() {
        return toAjax(sysPnDictionaryService.reloadCache());
    }

    @PreAuthorize("@ss.hasPermi('system:pndict:sync')")
    @GetMapping("/syncBasicLib")
    public AjaxResult syncBasicLib() {
        return toAjax(sysPnDictionaryService.resetSqlite());
    }


    @GetMapping("/getChildren/{code}")
    public AjaxResult getChildren(@PathVariable String code) {
        return AjaxResult.success(sysPnDictionaryService.getChildren(code,getCompanyId()));
    }

    @GetMapping("/getAllChildren/{code}")
    public AjaxResult getAllChildren(@PathVariable String code) {
        return AjaxResult.success(sysPnDictionaryService.getAllChildren(code,getCompanyId()));
    }
}
